package com.smileus.dal;

import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.common.util.Page;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.smileus.common.Env;

/**
 * FileName：BaseIbaitsDAO.java Date�?2010-12-6 User: heng.dong 
 */
@SuppressWarnings("unchecked")
public class BaseIbaitsDAO extends SqlMapClientTemplate implements BaseDAO {
	public static final int DEFAULT_BATCH_CLEAR_SIZE = 5000;

	public BaseIbaitsDAO(String sqlMapClientId) {
		super((SqlMapClient) Env.getBean(sqlMapClientId));
	}

	public BaseIbaitsDAO(SqlMapClient sqlMapClient) {
		super(sqlMapClient);
	}

	public BaseIbaitsDAO(Object sqlMapClient) {
		super((SqlMapClient) sqlMapClient);
	}

	/**
	 * method name:IbatisDao-batchOperation
	 * 
	 * @param sqlAlias
	 * @param list
	 */

	public void batchOperation(final String sqlAlias, final List list) {
		batchOperation(sqlAlias, list, DEFAULT_BATCH_CLEAR_SIZE);
	}

	/**
	 * method name:IbatisDao-batchOperation
	 * 
	 * @param sqlAlias
	 * @param list
	 * @param clearSize
	 */
	public void batchOperation(final String sqlAlias, final List list, final int clearSize) {
		try {
			execute(new SqlMapClientCallback() {
				public Object doInSqlMapClient(SqlMapExecutor sqlMapExecutor) throws SQLException {
					int size = list.size();
					Object param = null;
					sqlMapExecutor.startBatch();
					for (int i = 0; i < size; i++) {
						param = list.get(i);
						sqlMapExecutor.insert(sqlAlias, param);
						if ((i + 1) % clearSize == 0) {
							sqlMapExecutor.executeBatch();
						}
					}
					sqlMapExecutor.executeBatch();
					return null;
				}

			});
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * method name:IbatisDao-queryForPage
	 * 
	 * @param sqlAlias
	 * @param page
	 * @return
	 */
	public Page queryForPage(final String sqlAlias, final Page page) {
		Page result = null;
		try {
			result = getSqlMapClient().queryForPage(sqlAlias, page);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	/**
	 * method name:IbatisDao-queryForPage
	 * 
	 * @param sqlAlias
	 * @param page
	 * @param param
	 * @return
	 */
	public Page queryForPage(final String sqlAlias, final Page page, final Object param) {
		Page result = null;
		try {
			result = getSqlMapClient().queryForPage(sqlAlias, page, param);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return result;
	}

	public <T> T queryForEntity(String statementName, Class<T> targetClass) {
		return (T) queryForObject(statementName);
	}

	public <T> T queryForEntity(String statementName, Object param, Class<T> targetClass) {
		return (T) queryForObject(statementName, param);
	}

	public <T> List<T> queryForList(String statementName, Class<T> targetClass) {
		return queryForList(statementName);
	}

	public <T> List<T> queryForList(String statementName, Object param, Class<T> targetClass) {
		return queryForList(statementName, param);
	}

	public <T> Page<T> queryForPage(String statementName, Page page, Class<T> targetClass) {
		return queryForPage(statementName, page);
	}

	public <T> Page<T> queryForPage(String statementName, Page page, Object param, Class<T> targetClass) {
		return queryForPage(statementName, page, param);
	}
}
